/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Welcome;

import ImageCache.ImageCache;
import RMS.LoadImageInfoFromRecord;
import RMS.LoadImageFromRecord;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class LevelActivated extends Canvas implements Runnable {

    private static Image sample, nomalThumb, hoverThumb, failThumb;
    private Image bufferImage, live;
    private int x = 1, y = 1, question = 0, check = 1, setTime;
    private QuestionList q1;
    private Start midlet;
    protected int line = 1, aline = 1, LEVEL;
    protected boolean isMenu = false, isAnswer = false, goiY;
    private boolean one = false, two = false, three = false, four = false,
            five = false, six = false, seven = false, eight = false, nine;
    private boolean oneFailed = false, twoFailed = false, threeFailed = false,
            fourFailed = false, fiveFailed = false, sixFailed = false, sevenFailed = false, eightFailed = false, nineFailed;
    private boolean oneOpen = true, twoOpen = true, threeOpen = true, fourOpen = true,
            fiveOpen = true, sixOpen = true, sevenOpen = true, eightOpen = true, nineOpen = true;
    private String aof1, aof2, goii;
    private boolean next = false, running = true;
    private int failed, instanceKeyCode;
    private ReDownloadImage rdi;
    private LoadImageInfoFromRecord loadImage;
    private HuongDan1 huongDan1;
    private DownloadAlert dAlert;
    private HighScore hScore;
    private InsertAnswer ia;
    private HuongDan2 HD2;

    public LevelActivated(Start midlet, int setTime) {
        this.setFullScreenMode(true);
        DrawChoiceActivated.time = setTime;
        this.setTime = setTime;
        this.midlet = midlet;
        this.live = DrawChoice.live;
        QuestionList.imgId = 0;

        bufferImage = Image.createImage(getWidth(), getHeight());
        hScore = new HighScore(midlet);
        ia = new InsertAnswer(midlet);
        HD2 = new HuongDan2(midlet);
        dAlert = new DownloadAlert(midlet, question);

//        if (LoadImageFromRecord.numbOfRecord() < 10) {
//            midlet.getDis().setCurrent(rdi);
//        } else {
        try {
            sample = LoadImageFromRecord.getImage(DrawChoice.VD[9]);
            System.out.println("width:" + sample.getWidth() + ",height:" + sample.getHeight());
            loadImage.loadRecord(DrawChoice.VD[9]);
            aof1 = LoadImageInfoFromRecord.getVresultSign();
            goii = LoadImageInfoFromRecord.getVsuggest();
            aof2 = LoadImageInfoFromRecord.getVresultText();
        } catch (NullPointerException ex) {
            System.out.println("2");
            midlet.getDis().setCurrent(rdi);
        }

        if (sample != null) {
            huongDan1 = new HuongDan1(midlet, goii);
            if (sample.getHeight() > getHeight() / 2) {
                sample = ImageScaled.CreateScaledImage(sample, getHeight() / 2, getHeight() / 2);
            }
            int width = sample.getWidth() / 3 + 1;
            int height = sample.getHeight() / 3 + 1;

            if (nomalThumb == null) {
                nomalThumb = ImageScaled.CreateScaledImage(DrawChoice.q_nomal, width, height);
            }
            if (hoverThumb == null) {
                hoverThumb = ImageScaled.CreateScaledImage(DrawChoice.q_hover, width, height);
            }
            if (failThumb == null) {
                failThumb = ImageScaled.CreateScaledImage(DrawChoice.q_failed, width, height);
            }
        } else {
            rdi = new ReDownloadImage(midlet);
            midlet.getDis().setCurrent(rdi);
        }
//        }

        Thread t = new Thread(this);
        t.start();
    }

    public void run() {
        long point = 0;
        int i = 1, j = 1;
        boolean timeout = false, setPoint = true;
        while (running) {

            if (question == 0) {
                i = 1;
                setPoint = true;
            } else {
                if (setPoint) {
                    point = System.currentTimeMillis();
                    setPoint = false;
                }
                if (System.currentTimeMillis() - point >= 1000 * i) {
                    System.out.println("in");
                    if (DrawChoiceActivated.time > 0) {
                        DrawChoiceActivated.time--;
                        j = 1;
                        repaint();

                    } else {
                        timeout = true;
                    }
                    i++;
                }
                if (timeout && j == 1 && DrawChoiceActivated.time == 0) {
                    timeOut();
                    j = 2;
                    timeout = false;
                }
            }
        }
    }

    private void timeOut() {
        Alert fail = new Alert("", "Hết thời gian", null, AlertType.INFO);
        fail.setTimeout(1000);
        midlet.getDis().setCurrent(fail, this);
        PassTextboxText.setTextBoxText("");

        if (x == 1 && y == 1) {
            one = true;
            oneOpen = false;
            oneFailed = true;
            failed++;
        }
        if (x == 2 && y == 1) {
            two = true;
            twoOpen = false;
            twoFailed = true;
            failed++;
        }
        if (x == 3 && y == 1) {
            three = true;
            threeOpen = false;
            threeFailed = true;
            failed++;
        }
        if (x == 1 && y == 2) {
            four = true;
            fourOpen = false;
            fourFailed = true;
            failed++;
        }
        if (x == 2 & y == 2) {
            five = true;
            fiveOpen = false;
            fiveFailed = true;
            failed++;
        }
        if (x == 3 && y == 2) {
            six = true;
            sixOpen = false;
            sixFailed = true;
            failed++;
        }
        if (x == 1 && y == 3) {
            seven = true;
            sevenOpen = false;
            sevenFailed = true;
            failed++;
        }
        if (x == 2 && y == 3) {
            eight = true;
            eightOpen = false;
            eightFailed = true;
            failed++;
        }
        if (x == 3 && y == 3) {
            nine = true;
            nineOpen = false;
            nineFailed = true;
            failed++;
        }
        isMenu = false;
        question = 0;
        repaint();
    }

    protected void paint(Graphics g) {
        if (DrawChoice.pass) {
            check4Answer();
        } else {
            if (DrawChoice.bigAnswer) {
                String answer = PassTextboxText.getTextBoxText();
                System.out.println(answer + "  " + aof1 + "  " + aof2);
                if (answer.equalsIgnoreCase(aof1) || answer.equalsIgnoreCase(aof2)) {
                    next = true;
                    DrawChoice.score += 20;
                    PassTextboxText.setTextBoxText("");
                    victory();
                } else {
                    Alert fail = new Alert("", "Chưa chính xác", null, AlertType.INFO);
                    fail.setTimeout(1000);
                    midlet.getDis().setCurrent(fail);
                    DrawChoice.liv--;
                    PassTextboxText.setTextBoxText("");
                    if (DrawChoice.liv == 0) {
                        Alert gameOver = new Alert("Game Over", "Bạn đã thua !", null, AlertType.INFO);
                        gameOver.setTimeout(Alert.FOREVER);
                        midlet.getDis().setCurrent(gameOver, new ScoreSave(midlet, true, DrawChoice.score));
                    }
                }
                DrawChoice.bigAnswer = false;
            }
            if (!next) {
                if (failed == 9) {
                    sample = null;
                    hoverThumb = null;
                    nomalThumb = null;
                    failThumb = null;
                    System.gc();
                    Alert gameOver = new Alert("Game Over", "Bạn đã thua !", null, AlertType.INFO);
                    gameOver.setTimeout(Alert.FOREVER);
                    midlet.getDis().setCurrent(gameOver, new ScoreSave(midlet, true, DrawChoice.score));
                } else {
                    if (sample != null) {
                        if (DrawChoice.ans) {
                            check4Answer();
                        } else {
                            renderWorld();
                        }
                    } else {
                        rdi = new ReDownloadImage(midlet);
                        midlet.getDis().setCurrent(rdi);
                    }
                }
            }
            g.drawImage(bufferImage, 0, 0, Graphics.LEFT | Graphics.TOP);
        }
    }

    private void victory() {
        Alert a = new Alert("Chúc mừng", "Bạn đã vượt qua màn Về đích. Chọn Lên đời để chơi tiếp", null, AlertType.INFO);
        a.setTimeout(Alert.FOREVER);
        midlet.getDis().setCurrent(a);

        Graphics g = bufferImage.getGraphics();
        Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        g.setFont(font);
        g.setColor(0x00000000);
        g.fillRect(0, 0, getWidth(), getHeight());

        g.setColor(0x00EAEE0A);
        g.fillRect(5, 5, getWidth() - 10, getHeight() - font.getHeight() - 12);

        g.setColor(0x00DD6F00);
        g.fillRect(0, getHeight() - font.getHeight() - 2, getWidth(), font.getHeight() + 2);

        g.setColor(0xffffff);
        g.drawLine(0, getHeight() - font.getHeight() - 2, getWidth(), getHeight() - font.getHeight() - 2);
        g.drawString("Lên đời", getWidth() - 1, getHeight() - 1, Graphics.RIGHT | Graphics.BOTTOM);
        g.drawString("Thoát", 1, getHeight() - 1, Graphics.LEFT | Graphics.BOTTOM);
        g.setColor(0x000000);
        g.drawString("Congratulation !", getWidth() / 2, 10, Graphics.HCENTER | Graphics.TOP);
        g.drawImage(sample, getWidth() / 2, 15 + font.getHeight(), Graphics.HCENTER | Graphics.TOP);
        g.setColor(255, 0, 0);
        g.drawString("Điểm số của bạn " + String.valueOf(DrawChoice.score), getWidth() / 2, 20 + font.getHeight()
                + sample.getHeight(), Graphics.HCENTER | Graphics.TOP);
    }

    private void renderWorld() {

        Graphics g = bufferImage.getGraphics();
        g.setColor(0x00f0dfb6);
        g.fillRect(0, 0, getWidth(), getHeight());

        if (question == 0) {

            g.setColor(0x00f8efdb);
            Font f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
            g.setFont(f);
            g.fillRoundRect((getWidth() - sample.getWidth()) / 2, (getHeight() - sample.getHeight()) / 2 - 25 - f.getHeight(),
                    sample.getWidth() / 2 - 3, f.getHeight() + 10, 5, 5);
            g.fillRoundRect(getWidth() / 2 + 3, (getHeight() - sample.getHeight()) / 2 - 25 - f.getHeight(),
                    sample.getWidth() / 2 - 3, f.getHeight() + 10, 5, 5);
            g.setColor(0x000000);
            g.drawString("BÀI 4", getWidth() / 2 - sample.getWidth() / 4, (getHeight() - sample.getHeight()) / 2 - 22,
                    Graphics.HCENTER | Graphics.BASELINE);
            g.drawString(String.valueOf(DrawChoice.score), getWidth() / 2 + sample.getWidth() / 4, (getHeight() - sample.getHeight()) / 2 - 22,
                    Graphics.HCENTER | Graphics.BASELINE);

            g.setColor(0x00f6bd35);
            g.fillRoundRect((getWidth() - sample.getWidth()) / 2 - 10, (getHeight() - sample.getHeight()) / 2 - 10,
                    sample.getWidth() + 20, sample.getHeight() + 20, 5, 5);
//            draw Menu
            g.setColor(0x00b0711f);
            Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
            g.setFont(font);
            g.fillRect(0, getHeight() - font.getHeight() - 4, getWidth(), font.getHeight() + 4);
            g.setColor(0x00ffffff);
            g.drawLine(0, getHeight() - font.getHeight() - 4, getWidth(), getHeight() - font.getHeight() - 4);
            g.drawString("Menu", 1, getHeight() - 2, Graphics.LEFT | Graphics.BOTTOM);
            if (one && two && three && four && five && six && seven && eight && nine && !isMenu) {
                g.drawString("Bỏ qua", getWidth() - 1, getHeight() - 2, Graphics.RIGHT | Graphics.BOTTOM);
            } else {
                g.drawString("Chọn", getWidth() - 1, getHeight() - 2, Graphics.RIGHT | Graphics.BOTTOM);
            }

            if (DrawChoice.liv == 3) {
                g.drawImage(live, getWidth() / 2, (getHeight() + sample.getHeight()) / 2 + 11,
                        Graphics.HCENTER | Graphics.TOP);
                g.drawImage(live, getWidth() / 2 - live.getWidth() - 2, (getHeight() + sample.getHeight()) / 2 + 11,
                        Graphics.HCENTER | Graphics.TOP);
                g.drawImage(live, getWidth() / 2 + live.getWidth() + 2, (getHeight() + sample.getHeight()) / 2 + 11,
                        Graphics.HCENTER | Graphics.TOP);
            } else if (DrawChoice.liv == 2) {
                g.drawImage(live, getWidth() / 2, (getHeight() + sample.getHeight()) / 2 + 11,
                        Graphics.HCENTER | Graphics.TOP);
                g.drawImage(live, getWidth() / 2 - live.getWidth() - 2, (getHeight() + sample.getHeight()) / 2 + 11,
                        Graphics.HCENTER | Graphics.TOP);
            } else if (DrawChoice.liv == 1) {
                g.drawImage(live, getWidth() / 2 - live.getWidth() - 2, (getHeight() + sample.getHeight()) / 2 + 11,
                        Graphics.HCENTER | Graphics.TOP);
            }

            g.drawImage(sample, getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);

            if (!one) {
                //                g.fillRect((getWidth() - sample.getWidth()) / 2, (getHeight() - sample.getHeight()) / 2,
                //                        sample.getWidth() / 2, sample.getHeight() / 2);

                g.drawImage(nomalThumb, (getWidth() - sample.getWidth()) / 2,
                        (getHeight() - sample.getHeight()) / 2, Graphics.LEFT | Graphics.TOP);

            }

            if (oneFailed) {
                g.drawImage(failThumb, (getWidth() - sample.getWidth()) / 2,
                        (getHeight() - sample.getHeight()) / 2, Graphics.LEFT | Graphics.TOP);
            }

            g.setColor(0x00DDB19C);
            if (x == 1 && y == 1) {
                if (!one) {
                    g.drawImage(hoverThumb, (getWidth() - sample.getWidth()) / 2,
                            (getHeight() - sample.getHeight()) / 2, Graphics.LEFT | Graphics.TOP);

                }
                g.setColor(0x00ED3838);
                g.drawRect((getWidth() - sample.getWidth()) / 2 + 1, (getHeight() - sample.getHeight()) / 2 + 1,
                        sample.getWidth() / 3 - 2, sample.getHeight() / 3 - 2);
                g.setColor(0x00D17B53);
            }


            if (!two) {
//                g.fillRect(getWidth() / 2, (getHeight() - sample.getHeight()) / 2,
//                        sample.getWidth() / 2, sample.getHeight() / 2);
                g.drawImage(nomalThumb, getWidth() / 2 - sample.getWidth() / 6 - 1,
                        (getHeight() - sample.getHeight()) / 2, Graphics.LEFT | Graphics.TOP);
            }
            if (twoFailed) {
                g.drawImage(failThumb, getWidth() / 2 - sample.getWidth() / 6 - 1,
                        (getHeight() - sample.getHeight()) / 2, Graphics.LEFT | Graphics.TOP);
            }
            g.setColor(0x00DDB19C);
            if (x == 2 && y == 1) {
                if (!two) {
                    g.drawImage(hoverThumb, getWidth() / 2 - sample.getWidth() / 6 - 1,
                            (getHeight() - sample.getHeight()) / 2, Graphics.LEFT | Graphics.TOP);
                }
                g.setColor(0x00ED3838);
                g.drawRect(getWidth() / 2 - sample.getWidth() / 6, (getHeight() - sample.getHeight()) / 2 + 1,
                        sample.getWidth() / 3 - 2, sample.getHeight() / 3 - 2);
                g.setColor(0x00D17B53);
            }

            if (!three) {
//                g.fillRect((getWidth() - sample.getWidth()) / 2, getHeight() / 2,
//                        sample.getWidth() / 2, sample.getHeight() / 2);
                g.drawImage(nomalThumb, getWidth() / 2 + sample.getWidth() / 6,
                        (getHeight() - sample.getHeight()) / 2, Graphics.LEFT | Graphics.TOP);
            }
            if (threeFailed) {
                g.drawImage(failThumb, getWidth() / 2 + sample.getWidth() / 6,
                        (getHeight() - sample.getHeight()) / 2, Graphics.LEFT | Graphics.TOP);
            }
            g.setColor(0x00DDB19C);
            if (x == 3 && y == 1) {
                if (!three) {
                    g.drawImage(hoverThumb, getWidth() / 2 + sample.getWidth() / 6,
                            (getHeight() - sample.getHeight()) / 2, Graphics.LEFT | Graphics.TOP);
                }
                g.setColor(0x00ED3838);
                g.drawRect(getWidth() / 2 + sample.getWidth() / 6 + 1, (getHeight() - sample.getHeight()) / 2 + 1,
                        sample.getWidth() / 3 - 2, sample.getHeight() / 3 - 2);
                g.setColor(0x00D17B53);
            }

            if (!four) {
//                g.fillRect(getWidth() / 2, getHeight() / 2,
//                        sample.getWidth() / 2, sample.getHeight() / 2);
                g.drawImage(nomalThumb, (getWidth() - sample.getWidth()) / 2, getHeight() / 2 - sample.getHeight() / 6, Graphics.LEFT | Graphics.TOP);
            }
            if (fourFailed) {
                g.drawImage(failThumb, (getWidth() - sample.getWidth()) / 2, getHeight() / 2 - sample.getHeight() / 6, Graphics.LEFT | Graphics.TOP);
            }
            g.setColor(0x00DDB19C);
            if (x == 1 && y == 2) {
                if (!four) {
                    g.drawImage(hoverThumb, (getWidth() - sample.getWidth()) / 2, getHeight() / 2 - sample.getHeight() / 6, Graphics.LEFT | Graphics.TOP);
                }
                g.setColor(0x00ED3838);
                g.drawRect((getWidth() - sample.getWidth()) / 2 + 1, getHeight() / 2 - sample.getHeight() / 6 + 1,
                        sample.getWidth() / 3 - 2, sample.getHeight() / 3 - 2);
                g.setColor(0x00D17B53);
            }

            if (!five) {
//                g.fillRect(getWidth() / 2, getHeight() / 2,
//                        sample.getWidth() / 2, sample.getHeight() / 2);
                g.drawImage(nomalThumb, getWidth() / 2 - sample.getWidth() / 6 - 1, getHeight() / 2 - sample.getHeight() / 6, Graphics.LEFT | Graphics.TOP);
            }
            if (fiveFailed) {
                g.drawImage(failThumb, getWidth() / 2 - sample.getWidth() / 6 - 1, getHeight() / 2 - sample.getHeight() / 6, Graphics.LEFT | Graphics.TOP);
            }
            g.setColor(0x00DDB19C);
            if (x == 2 && y == 2) {
                if (!five) {
                    g.drawImage(hoverThumb, getWidth() / 2 - sample.getWidth() / 6 - 1, getHeight() / 2 - sample.getHeight() / 6, Graphics.LEFT | Graphics.TOP);
                }
                g.setColor(0x00ED3838);
                g.drawRect(getWidth() / 2 - sample.getWidth() / 6, getHeight() / 2 - sample.getHeight() / 6 + 1,
                        sample.getWidth() / 3 - 2, sample.getHeight() / 3 - 2);
                g.setColor(0x00D17B53);
            }

            if (!six) {
//                g.fillRect(getWidth() / 2, getHeight() / 2,
//                        sample.getWidth() / 2, sample.getHeight() / 2);
                g.drawImage(nomalThumb, getWidth() / 2 + sample.getWidth() / 6, getHeight() / 2 - sample.getHeight() / 6, Graphics.LEFT | Graphics.TOP);
            }
            if (sixFailed) {
                g.drawImage(failThumb, getWidth() / 2 + sample.getWidth() / 6, getHeight() / 2 - sample.getHeight() / 6, Graphics.LEFT | Graphics.TOP);
            }
            g.setColor(0x00DDB19C);
            if (x == 3 && y == 2) {
                if (!six) {
                    g.drawImage(hoverThumb, getWidth() / 2 + sample.getWidth() / 6, getHeight() / 2 - sample.getHeight() / 6, Graphics.LEFT | Graphics.TOP);
                }
                g.setColor(0x00ED3838);
                g.drawRect(getWidth() / 2 + sample.getWidth() / 6 + 1, getHeight() / 2 - sample.getHeight() / 6 + 1,
                        sample.getWidth() / 3 - 2, sample.getHeight() / 3 - 2);
                g.setColor(0x00D17B53);
            }

            if (!seven) {
//                g.fillRect(getWidth() / 2, getHeight() / 2,
//                        sample.getWidth() / 2, sample.getHeight() / 2);
                g.drawImage(nomalThumb, (getWidth() - sample.getWidth()) / 2, getHeight() / 2 + sample.getHeight() / 6, Graphics.LEFT | Graphics.TOP);
            }
            if (sevenFailed) {
                g.drawImage(failThumb, (getWidth() - sample.getWidth()) / 2, getHeight() / 2 + sample.getHeight() / 6, Graphics.LEFT | Graphics.TOP);
            }
            g.setColor(0x00DDB19C);
            if (x == 1 && y == 3) {
                if (!seven) {
                    g.drawImage(hoverThumb, (getWidth() - sample.getWidth()) / 2, getHeight() / 2 + sample.getHeight() / 6, Graphics.LEFT | Graphics.TOP);
                }
                g.setColor(0x00ED3838);
                g.drawRect((getWidth() - sample.getWidth()) / 2 + 1, getHeight() / 2 + sample.getHeight() / 6 + 1,
                        sample.getWidth() / 3 - 2, sample.getHeight() / 3 - 2);
                g.setColor(0x00D17B53);
            }

            if (!eight) {
//                g.fillRect(getWidth() / 2, getHeight() / 2,
//                        sample.getWidth() / 2, sample.getHeight() / 2);
                g.drawImage(nomalThumb, getWidth() / 2 - sample.getWidth() / 6 - 1, getHeight() / 2 + sample.getHeight() / 6, Graphics.LEFT | Graphics.TOP);
            }
            if (eightFailed) {
                g.drawImage(failThumb, getWidth() / 2 - sample.getWidth() / 6 - 1, getHeight() / 2 + sample.getHeight() / 6, Graphics.LEFT | Graphics.TOP);
            }
            g.setColor(0x00DDB19C);
            if (x == 2 && y == 3) {
                if (!eight) {
                    g.drawImage(hoverThumb, getWidth() / 2 - sample.getWidth() / 6 - 1, getHeight() / 2 + sample.getHeight() / 6, Graphics.LEFT | Graphics.TOP);
                }
                g.setColor(0x00ED3838);
                g.drawRect(getWidth() / 2 - sample.getWidth() / 6, getHeight() / 2 + sample.getHeight() / 6 + 1,
                        sample.getWidth() / 3 - 2, sample.getHeight() / 3 - 2);
                g.setColor(0x00D17B53);
            }

            if (!nine) {
//                g.fillRect(getWidth() / 2, getHeight() / 2,
//                        sample.getWidth() / 2, sample.getHeight() / 2);
                g.drawImage(nomalThumb, getWidth() / 2 + sample.getWidth() / 6, getHeight() / 2 + sample.getHeight() / 6, Graphics.LEFT | Graphics.TOP);
            }
            if (nineFailed) {
                g.drawImage(failThumb, getWidth() / 2 + sample.getWidth() / 6, getHeight() / 2 + sample.getHeight() / 6, Graphics.LEFT | Graphics.TOP);
            }
            g.setColor(0x00DDB19C);
            if (x == 3 && y == 3) {
                if (!nine) {
                    g.drawImage(hoverThumb, getWidth() / 2 + sample.getWidth() / 6, getHeight() / 2 + sample.getHeight() / 6, Graphics.LEFT | Graphics.TOP);
                }
                g.setColor(0x00ED3838);
                g.drawRect(getWidth() / 2 + sample.getWidth() / 6 + 1, getHeight() / 2 + sample.getHeight() / 6 + 1,
                        sample.getWidth() / 3 - 2, sample.getHeight() / 3 - 2);
                g.setColor(0x00D17B53);
            }

            String s1 = "Giải hình lớn";
            String s2 = "Gợi ý";
            String s3 = "Lưu điểm";
            String s4 = "Thoát";
            if (isMenu) {
                //draw Menu
                font = Font.getDefaultFont();
                g.setColor(0x00000000);
                g.drawRect(0, getHeight() - 5 * font.getHeight() - 12, font.stringWidth(s1) + 10, 4 * font.getHeight() + 10);
                g.setColor(0x00F0E001);
                g.fillRect(0 + 1, getHeight() - 5 * font.getHeight() - 12 + 1, font.stringWidth(s1) + 9, 4 * font.getHeight() + 8);

                if (line == 1) {
                    g.setColor(0x0041F001);
                    g.fillRect(0 + 1, getHeight() - 5 * font.getHeight() - 12 + 1, font.stringWidth(s1) + 9, font.getHeight() + 3);
                }
                if (line == 2) {
                    g.setColor(0x0041F001);
                    g.fillRect(0 + 1, getHeight() - 4 * font.getHeight() - 10 + 1, font.stringWidth(s1) + 9, font.getHeight() + 3);
                }
                if (line == 3) {
                    g.setColor(0x0041F001);
                    g.fillRect(0 + 1, getHeight() - 3 * font.getHeight() - 8 + 1, font.stringWidth(s1) + 9, font.getHeight() + 3);
                }
                if (line == 4) {
                    g.setColor(0x0041F001);
                    g.fillRect(0 + 1, getHeight() - 2 * font.getHeight() - 6 + 1, font.stringWidth(s1) + 9, font.getHeight() + 3);
                }

                g.setFont(font);
                g.setColor(0x00000000);
                g.drawString(s4, 0 + 2, getHeight() - font.getHeight() - 3 - 2, Graphics.LEFT | Graphics.BASELINE);
                g.drawString(s3, 0 + 2, getHeight() - 2 * font.getHeight() - 3 - 2 * 2, Graphics.LEFT | Graphics.BASELINE);
                g.drawString(s2, 0 + 2, getHeight() - 3 * font.getHeight() - 3 - 2 * 3, Graphics.LEFT | Graphics.BASELINE);
                g.drawString(s1, 0 + 2, getHeight() - 4 * font.getHeight() - 3 - 2 * 4, Graphics.LEFT | Graphics.BASELINE);

            }
        } else {
            q1.render(g);

        }
    }

    private void check4Answer() {
        String answer = PassTextboxText.getTextBoxText();
        DrawChoice.ans = false;
        if (answer.equalsIgnoreCase(q1.getResultText()) || answer.equalsIgnoreCase(q1.getResultSign())) {
            if (x == 1 && y == 1) {
                one = true;
                oneOpen = false;

                if (goiY) {
                    DrawChoice.score += 5;
                    goiY = false;
                } else {
                    DrawChoice.score += 10;
                }
            }
            if (x == 2 && y == 1) {
                two = true;
                twoOpen = false;

                if (goiY) {
                    DrawChoice.score += 5;
                    goiY = false;
                } else {
                    DrawChoice.score += 10;
                }
            }
            if (x == 3 && y == 1) {
                three = true;
                threeOpen = false;

                if (goiY) {
                    DrawChoice.score += 5;
                    goiY = false;
                } else {
                    DrawChoice.score += 10;
                }
            }
            if (x == 1 && y == 2) {
                four = true;
                fourOpen = false;

                if (goiY) {
                    DrawChoice.score += 5;
                    goiY = false;
                } else {
                    DrawChoice.score += 10;
                }
            }
            if (x == 2 & y == 2) {
                five = true;
                fiveOpen = false;

                if (goiY) {
                    DrawChoice.score += 5;
                    goiY = false;
                } else {
                    DrawChoice.score += 10;
                }
            }
            if (x == 3 && y == 2) {
                six = true;
                sixOpen = false;

                if (goiY) {
                    DrawChoice.score += 5;
                    goiY = false;
                } else {
                    DrawChoice.score += 10;
                }
            }
            if (x == 1 && y == 3) {
                seven = true;
                sevenOpen = false;

                if (goiY) {
                    DrawChoice.score += 5;
                    goiY = false;
                } else {
                    DrawChoice.score += 10;
                }
            }
            if (x == 2 && y == 3) {
                eight = true;
                eightOpen = false;

                if (goiY) {
                    DrawChoice.score += 5;
                    goiY = false;
                } else {
                    DrawChoice.score += 10;
                }
            }
            if (x == 3 && y == 3) {
                nine = true;
                nineOpen = false;

                if (goiY) {
                    DrawChoice.score += 5;
                    goiY = false;
                } else {
                    DrawChoice.score += 10;
                }
            }

            isMenu = false;
            question = 0;

            midlet.disManager.pushDisplayable(this);
            HD2.createHD2(q1.getMean(), DrawChoice.score);
            midlet.getDis().setCurrent(HD2);

            repaint();
        } else {

            if (!DrawChoice.pass) {
                Alert fail = new Alert("", "Chưa chính xác", null, AlertType.INFO);
                fail.setTimeout(1000);
                midlet.getDis().setCurrent(fail, this);
            } else {
                DrawChoice.pass = false;
            }

            if (x == 1 && y == 1) {
                one = true;
                oneOpen = false;
                oneFailed = true;
                failed++;
            }
            if (x == 2 && y == 1) {
                two = true;
                twoOpen = false;
                twoFailed = true;
                failed++;
            }
            if (x == 3 && y == 1) {
                three = true;
                threeOpen = false;
                threeFailed = true;
                failed++;
            }
            if (x == 1 && y == 2) {
                four = true;
                fourOpen = false;
                fourFailed = true;
                failed++;
            }
            if (x == 2 & y == 2) {
                five = true;
                fiveOpen = false;
                fiveFailed = true;
                failed++;
            }
            if (x == 3 && y == 2) {
                six = true;
                sixOpen = false;
                sixFailed = true;
                failed++;
            }
            if (x == 1 && y == 3) {
                seven = true;
                sevenOpen = false;
                sevenFailed = true;
                failed++;
            }
            if (x == 2 && y == 3) {
                eight = true;
                eightOpen = false;
                eightFailed = true;
                failed++;
            }
            if (x == 3 && y == 3) {
                nine = true;
                nineOpen = false;
                nineFailed = true;
                failed++;
            }
            isMenu = false;
            question = 0;
            repaint();
        }
        PassTextboxText.setTextBoxText("");
    }

    private void softKeyLeftFunction() {

        if (next) {
            sample = null;
            hoverThumb = null;
            nomalThumb = null;
            failThumb = null;
            System.gc();
            midlet.disManager.Home();
        }

        if (question == 1) {
            if (q1.isMenu) {
                if (q1.line == 1) {
                    goiY = true;
                    Alert goiy = new Alert("Gợi ý", (String) loadImage.getVsuggest(),
                            null, AlertType.INFO);
                    goiy.setTimeout(Alert.FOREVER);
                    midlet.getDis().setCurrent(goiy, this);
                } else if (q1.line == 2) {
                    midlet.disManager.pushDisplayable(this);
                    midlet.disManager.pushDisplayable(DrawChoice.alertCon);
                }
            }

            q1.isMenu = !q1.isMenu;

            if (q1.isMenu) {
                q1.isAnswer = false;
            }
            q1.line = 1;
            repaint();
        } else {
            isMenu = !isMenu;
            line = 1;
            repaint();
        }
    }

    private void softKeyRightFunction() {

        if (next) {
            sample = null;
            hoverThumb = null;
            nomalThumb = null;
            failThumb = null;
            ImageCache.getInstance().clearImageCache();
            System.gc();
            DrawChoice.levelPlaying++;
            midlet.disManager.emptyStack();
            midlet.getDis().setCurrent(new DownloadAlert(midlet, DrawChoice.levelPlaying));
        }

        if (question == 1) {
            if (!q1.isMenu) {
                midlet.disManager.pushDisplayable(this);
                midlet.disManager.pushDisplayable(ia.insertAnswer(""));
            } else {
                q1.isMenu = false;
            }
        } else {
            if (!isMenu) {
                if (((x == 1 && y == 1 && oneOpen) || (x == 2 && y == 1 && twoOpen) || (x == 3 && y == 1 && threeOpen)
                        || (x == 1 && y == 2 && fourOpen) || (x == 2 && y == 2 && fiveOpen) || (x == 3 && y == 2 && sixOpen)
                        || (x == 1 && y == 3 && sevenOpen) || (x == 2 && y == 3 && eightOpen) || (x == 3 && y == 3 && nineOpen))) {
                    question = 1;
                    q1 = new QuestionList(midlet, this, setTime);
                    repaint();
                }
                if (one && two && three && four && five && six && seven && eight && nine && !isMenu) {
                    sample = null;
                    ImageCache.getInstance().clearImageCache();
                    System.gc();
                    DrawChoice.levelPlaying++;
                    midlet.disManager.pushDisplayable(new DownloadAlert(midlet, DrawChoice.levelPlaying));
                }
            } else {
                if (line == 2) {
                    isMenu = false;
                    line = 1;
                    question = 0;
                    midlet.disManager.pushDisplayable(this);
                    midlet.disManager.pushDisplayable(huongDan1);
                } else if (line == 1) {
                    if ((one & !oneFailed) || (two & !twoFailed) || (three & !threeFailed) || (four & !fourFailed)
                            || (five & !fiveFailed) || (six & !sixFailed) || (seven & !sevenFailed) || (eight & !eightFailed) || (nine & !nineFailed)) {
                        isMenu = false;
                        line = 1;
                        question = 0;
                        DrawChoice.bigAnswer = true;
                        midlet.disManager.pushDisplayable(this);

                        midlet.disManager.pushDisplayable(ia.insertAnswer("Giải hình lớn"));
                    }
                } //Menu Luu diem
                else if (line == 3) {
                    midlet.disManager.pushDisplayable(new ScoreSave(midlet, false, DrawChoice.score));
                } else if (line == 4) {
                    DrawChoice.score = 0;
                    sample = null;
                    hoverThumb = null;
                    nomalThumb = null;
                    failThumb = null;
                    hScore = null;
                    huongDan1 = null;
                    ia = null;
                    System.gc();

                    midlet.disManager.home();
                }
            }
        }
    }

    private void upKeyFunction() {

        if (question == 0) {
            line--;
            if (line == 0) {
                line = 4;
            }
            repaint();
            if (y > 1 && !isMenu) {
                y--;
                repaint();
            }
        } else {
            if (q1.isMenu) {
                q1.line--;
                if (q1.line == 0) {
                    q1.line = 2;
                }
                repaint();
            } else if (q1.trans != 0) {
                if (q1.trans < q1.f.getHeight() + 4 - q1.y) {
                    q1.change = false;
                    q1.trans += 5;
                    if (q1.trans == 0) {
                        q1.trans = 1;
                    }
                    repaint();
                }
            }
        }
    }

    private void downKeyFunction() {

        if (question == 0) {
            line++;
            if (line == 5) {
                line = 1;
            }
            repaint();
            if (y < 3 && !isMenu) {
                y++;
                repaint();
            }
        } else {

            if (q1.isMenu) {
                q1.line++;
                if (q1.line == 3) {
                    q1.line = 1;
                }
                repaint();
            } else if (q1.trans != 0) {
                q1.change = false;
                q1.trans -= 5;
                if (q1.trans == 0) {
                    q1.trans = 1;
                }
                repaint();
            }
        }
    }

    private void centerKeyFunction() {
        if (next) {
            sample = null;
            hoverThumb = null;
            nomalThumb = null;
            failThumb = null;
            System.gc();
            ImageCache.getInstance().clearImageCache();
            System.gc();
            DrawChoice.levelPlaying++;
            midlet.disManager.emptyStack();
            midlet.getDis().setCurrent(new DownloadAlert(midlet, DrawChoice.levelPlaying));
        }

        if (question == 1) {
            if (q1.isMenu) {
                q1.isMenu = false;
                if (q1.line == 1) {
                    goiY = true;
                    Alert goiy = new Alert("Gợi ý", (String) loadImage.getVsuggest(),
                            null, AlertType.INFO);
                    goiy.setTimeout(Alert.FOREVER);
                    midlet.getDis().setCurrent(goiy, this);
                } else if (q1.line == 2) {
                    midlet.disManager.pushDisplayable(this);
                    midlet.disManager.pushDisplayable(DrawChoice.alertCon);
                }
            } else {
                if (PassTextboxText.getTextBoxText().equals("")) {
                    midlet.disManager.pushDisplayable(this);
                    InsertAnswer ia = new InsertAnswer(midlet);
                    midlet.disManager.pushDisplayable(ia.insertAnswer(""));
                }
            }
        } else {
            if (!isMenu) {
                if (((x == 1 && y == 1 && oneOpen) || (x == 2 && y == 1 && twoOpen) || (x == 3 && y == 1 && threeOpen)
                        || (x == 1 && y == 2 && fourOpen) || (x == 2 && y == 2 && fiveOpen) || (x == 3 && y == 2 && sixOpen)
                        || (x == 1 && y == 3 && sevenOpen) || (x == 2 && y == 3 && eightOpen) || (x == 3 && y == 3 && nineOpen))) {
                    question = 1;
                    q1 = new QuestionList(midlet, this, setTime);
                    repaint();
                }
            } else {
                if (line == 2) {
                    isMenu = false;
                    line = 1;
                    question = 0;
                    midlet.disManager.pushDisplayable(this);
                    midlet.disManager.pushDisplayable(huongDan1);
                } else if (line == 1) {
                    if ((one & !oneFailed) || (two & !twoFailed) || (three & !threeFailed) || (four & !fourFailed)
                            || (five & !fiveFailed) || (six & !sixFailed) || (seven & !sevenFailed) || (eight & !eightFailed) || (nine & !nineFailed)) {
                        isMenu = false;
                        line = 1;
                        question = 0;
                        DrawChoice.bigAnswer = true;
                        midlet.disManager.pushDisplayable(this);
                        InsertAnswer ia = new InsertAnswer(midlet);
                        midlet.disManager.pushDisplayable(ia.insertAnswer("Giải hình lớn"));
                    }
                } //Menu Luu diem
                else if (line == 3) {
                    midlet.disManager.pushDisplayable(new ScoreSave(midlet, false, DrawChoice.score));
                } else if (line == 4) {
                    DrawChoice.score = 0;

                    sample = null;
                    hoverThumb = null;
                    nomalThumb = null;
                    failThumb = null;
                    hScore = null;
                    huongDan1 = null;
                    ia = null;
                    System.gc();
                    midlet.disManager.home();
                }
            }
        }
    }

    protected void keyPressed(int keyCode) {
        instanceKeyCode = KeyCodeAdapter.getInstance().adoptKeyCode(keyCode);
        switch (instanceKeyCode) {
            case KeyCodeAdapter.KEY_1:
            case KeyCodeAdapter.SOFT_KEY_LEFT:
                softKeyLeftFunction();
                break;

            case KeyCodeAdapter.KEY_3:
            case KeyCodeAdapter.SOFT_KEY_RIGHT:
                softKeyRightFunction();
                break;

            case KeyCodeAdapter.KEY_2:
            case KeyCodeAdapter.UP_KEY:
                upKeyFunction();
                break;

            case KeyCodeAdapter.KEY_8:
            case KeyCodeAdapter.DOWN_KEY:
                downKeyFunction();
                break;

            case KeyCodeAdapter.KEY_6:
            case KeyCodeAdapter.RIGHT_KEY:

                repaint();
                if (x < 3 && !isMenu && question == 0) {
                    x++;
                    repaint();
                }
                break;

            case KeyCodeAdapter.KEY_4:
            case KeyCodeAdapter.LEFT_KEY:

                repaint();
                if (x > 1 && !isMenu && question == 0) {
                    x--;
                    repaint();
                }
                break;

            case KeyCodeAdapter.KEY_5:
            case KeyCodeAdapter.CENTER_KEY:
                centerKeyFunction();
                break;
        }
    }

    protected void pointerPressed(int x, int y) {
        Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);

        if (question == 0) {
            if (!isMenu) {
                if (x >= (getWidth() - sample.getWidth()) / 2 && x <= getWidth() / 2 - sample.getWidth() / 6) {
                    if (y < getHeight() / 2 - sample.getHeight() / 6 && y >= (getHeight() - sample.getHeight()) / 2) {
                        this.x = this.y = 1;
                        if (oneOpen) {
                            question = 1;
                            q1 = new QuestionList(midlet, this, setTime);
                            repaint();
                        }
                    } else if (y >= getHeight() / 2 - sample.getHeight() / 6 && y < getHeight() / 2 + sample.getHeight() / 6) {
                        this.x = 1;
                        this.y = 2;
                        if (fourOpen) {
                            question = 1;
                            q1 = new QuestionList(midlet, this, setTime);
                            repaint();
                        }
                    } else if (y >= getHeight() / 2 + sample.getHeight() / 6 && y < (getHeight() + sample.getHeight()) / 2) {
                        this.x = 1;
                        this.y = 3;
                        if (sevenOpen) {
                            question = 1;
                            q1 = new QuestionList(midlet, this, setTime);
                            repaint();
                        }
                    }
                } else if (x < getWidth() / 2 + sample.getWidth() / 6 && x >= getWidth() / 2 - sample.getWidth() / 6) {
                    if (y < getHeight() / 2 - sample.getHeight() / 6 && y >= (getHeight() - sample.getHeight()) / 2) {
                        this.x = 2;
                        this.y = 1;
                        if (twoOpen) {
                            question = 1;
                            q1 = new QuestionList(midlet, this, setTime);
                            repaint();
                        }
                    } else if (y >= getHeight() / 2 - sample.getHeight() / 6 && y < getHeight() / 2 + sample.getHeight() / 6) {
                        this.x = 2;
                        this.y = 2;
                        if (fiveOpen) {
                            question = 1;
                            q1 = new QuestionList(midlet, this, setTime);
                            repaint();
                        }
                    } else if (y >= getHeight() / 2 + sample.getHeight() / 6 && y < (getHeight() + sample.getHeight()) / 2) {
                        this.x = 2;
                        this.y = 3;
                        if (eightOpen) {
                            question = 1;
                            q1 = new QuestionList(midlet, this, setTime);
                            repaint();
                        }
                    }
                } else if (x > getWidth() / 2 + sample.getWidth() / 6 && x < (getWidth() + sample.getWidth()) / 2) {
                    if (y < getHeight() / 2 - sample.getHeight() / 6 && y >= (getHeight() - sample.getHeight()) / 2) {
                        this.x = 3;
                        this.y = 1;
                        if (threeOpen) {
                            question = 1;
                            q1 = new QuestionList(midlet, this, setTime);
                            repaint();
                        }
                    } else if (y >= getHeight() / 2 - sample.getHeight() / 6 && y < getHeight() / 2 + sample.getHeight() / 6) {
                        this.x = 3;
                        this.y = 2;
                        if (sixOpen) {
                            question = 1;
                            q1 = new QuestionList(midlet, this, setTime);
                            repaint();
                        }
                    } else if (y >= getHeight() / 2 + sample.getHeight() / 6 && y < (getHeight() + sample.getHeight()) / 2) {
                        this.x = 3;
                        this.y = 3;
                        if (nineOpen) {
                            question = 1;
                            q1 = new QuestionList(midlet, this, setTime);
                            repaint();
                        }
                    }
                }
            } else {
                font = Font.getDefaultFont();
                if (x >= 0 && x <= font.stringWidth("Giải hình lớn") + 20) {
                    if (y >= getHeight() - 5 * font.getHeight() - 12 + 1 && y < getHeight() - 4 * font.getHeight() - 10 + 1) {
                        if (((one & !oneFailed) || (two & !twoFailed) || (three & !threeFailed) || (four & !fourFailed)
                                || (five & !fiveFailed) || (six & !sixFailed) || (seven & !sevenFailed) || (eight & !eightFailed) || (nine & !nineFailed))) {
                            isMenu = false;
                            line = 1;
                            question = 0;
                            DrawChoice.bigAnswer = true;
                            midlet.disManager.pushDisplayable(this);

                            midlet.disManager.pushDisplayable(ia.insertAnswer("Giải hình lớn"));
                        }
                    } else if (y >= getHeight() - 4 * font.getHeight() - 10 + 1 && y < getHeight() - 3 * font.getHeight() - 8 + 1) {
                        isMenu = false;
                        line = 1;
                        question = 0;
                        midlet.disManager.pushDisplayable(this);
                        midlet.disManager.pushDisplayable(huongDan1);
                    } else if (y >= getHeight() - 3 * font.getHeight() - 8 + 1 && y < getHeight() - 2 * font.getHeight() - 6 + 1) {
                        isMenu = false;
                        midlet.disManager.pushDisplayable(this);
                        midlet.disManager.pushDisplayable(new ScoreSave(midlet, false, DrawChoice.score));
                    } else if (y >= getHeight() - 2 * font.getHeight() - 6 + 1 && y < getHeight() - font.getHeight() - 4 + 1) {
                        DrawChoice.score = 0;
                        DrawChoice.logo = DrawChoice.lock = DrawChoice.unlock = null;
                        hScore = null;
                        huongDan1 = null;
                        ia = null;
                        System.gc();

                        midlet.disManager.home();
                    }
                }
            }
        } else {
            if (x >= 0 && x <= font.stringWidth("Hướng dẫn") + 10) {
                if (q1.isMenu) {
                    if (y >= getHeight() - 3 * font.getHeight() - 10 && y < getHeight() - 2 * font.getHeight() - 8) {
                        goiY = true;
                        q1.isMenu = false;
                        Alert goiy = new Alert("Gợi ý", (String) loadImage.getVsuggest(),
                                null, AlertType.INFO);
                        goiy.setTimeout(Alert.FOREVER);
                        midlet.getDis().setCurrent(goiy, this);
                    } else if (y >= getHeight() - 2 * font.getHeight() - 8 && y < getHeight() - font.getHeight()) {
                        q1.isMenu = false;
                        midlet.disManager.pushDisplayable(this);
                        midlet.disManager.pushDisplayable(DrawChoice.alertCon);
                    }
                }
            }
        }
        if (y <= getHeight() && y >= getHeight() - font.getHeight() - 10) {
            if (x <= getWidth() && x >= getWidth() - font.stringWidth("Chọn đáp án") - 10) {
                softKeyRightFunction();
            }
            if (x >= 0 && x <= font.stringWidth("Menu") + 10) {
                softKeyLeftFunction();
            }
        }
    }

    public Start getMidlet() {
        return midlet;
    }
}
